import request from '../../utils/request';

Page({
    data: {
        recordList: [],
        currentUserRecord: null
    },

    getData() {
        let that = this;
        request({
            url: 'contractlog/contractlog/list',
            method: 'GET',
            data: { pageSize: 10000000, pageNum: 1 },
            success: (res) => {
                const userId = wx.getStorageSync('userid');

                if (res.data && res.data.rows) {
                    const recordList = res.data.rows.map(item => ({
                       ...item,
                        status: parseInt(item.status, 10)
                    }));

                    // 对记录进行去重处理，根据id去重
                    const uniqueRecordList = [];
                    const idSet = new Set();
                    recordList.forEach(item => {
                        if (!idSet.has(item.id)) {
                            idSet.add(item.id);
                            uniqueRecordList.push(item);
                        }
                    });

                    console.log(uniqueRecordList);

                    const currentUserRecord = uniqueRecordList.find(item => item.userId === userId);
                    this.setData({
                        recordList: uniqueRecordList,
                        currentUserRecord
                    });
                }
            }
        });
    },

    onLoad: function (options) {
        this.getData();
    },

    navigateToDetail: function (e) {
        const { id, status } = e.currentTarget.dataset;
        wx.navigateTo({
            url: `/pages/signdetail/signdetail?id=${id}&status=${status}`
        });
    }
});